var Event = function () {

    this.$grid_selector = $("#event-grid-table");
    this.pager_selector = "#event-grid-pager";

    this.URL = {
        REMOVE_ALL: '/system/event'
    };

    this.colModel = [
        { name: 'eventId', width: 80, fixed: true, key: true },
        { name: 'formattedMessage', width: 50, hidden: true },
        {
            width: 50, formatter: function (cellValue, row, data) {
                var msg = data.formattedMessage;
                return msg.substr(0, msg.length > 300 ? 300 : msg.length)
            }
        },
        { name: 'loggerName', width: 15 },
        { name: 'levelString', width: 80, fixed: true },
        {
            name: 'timestmp', width: 130, fixed: true, formatter: function (cellValue) {
                return new Date(cellValue).format("yyyy-MM-dd HH:mm:ss");
            }
        }
    ];
    this.options = {
        moduleKey: 'system/event',
        pager: this.pager_selector,
        sortname: 'timestmp',
        sortorder: 'desc',
        caption: "系统日志列表",
        colNames: ['日志ID', '详情', '详情', '名称', '级别', '创建时间'],
        colModel: this.colModel,
        navbarDefaultIcon: {
            add: false,
            edit: false,
            view: false,
            del: false
        }
    };
    this.detailView = '<div style="padding: 12px;">\
                        <dl>\
                            <dt>名称</dt>\
                            <dd>{{loggerName}}</dd>\
                            <dt>级别</dt>\
                            <dd>{{levelString}}</dd>\
                            <dt>创建时间</dt>\
                            <dd>{{timestmp}}</dd>\
                            <dt>详情</dt>\
                            <dd><pre class="prettyprint linenums">{{formattedMessage}}</pre></dd>\
                        </dl>\
                        </div>';
};

Event.prototype = {
    // 初始化表格
    initGird: function () {

        var self = this;
        this.$grid_selector
            .xumf_jqGird(this.options)
            // 编辑权限按钮初始化
         /*   .xumf_jqGird('navGrid', this.pager_selector).jqGrid('navButtonAdd', this.pager_selector,
            {
                caption: '',
                title: "删除全部",
                buttonicon: " fa fa-trash",
                onClickButton: function () {
                    self.deleteAll();
                },
                position: "first"
            })*/
            .xumf_jqGird('navGrid', this.pager_selector).jqGrid('navButtonAdd', this.pager_selector,
            {
                caption: '',
                title: "查看详情",
                buttonicon: "fa fa-exclamation-circle",
                onClickButton: function () {
                    $(this).getGridSelectedRow(function (rowData) {
                        layer.open({
                            title: '详情',
                            type: 1,
                            btn: ['确定'],
                            area: ['420px', '550px'], //宽高
                            content: _.template(self.detailView)(rowData)
                        });
                    });
                },
                position: "first"
            });


    },
    /**
     * 删除所有日志
     */
    deleteAll: function () {
        var self = this;
        layer.confirm('<div style="padding:12px;">您确定要清空所有日志吗？</div>', function (index, layero) {
            $.xumf.ajaxAndLoading({
                type: 'DELETE',
                url: self.URL.REMOVE_ALL
            }).success(function (res) {
                if (res.success) {
                    layer.close(index);
                    $('#log-grid-table').trigger('reloadGrid');
                }
                $.xumf.gritter(res);
            });
        });
    }
};

